﻿/// <reference path="../jquery-1.9.1.js" />
/// <reference path="../referraltracker.js" />

referraltracker.account.userpage = (function () {

    function UpdateUser() {
        var fname = $("#FirstName").val();
        var lname = $("#LastName").val();
        var email = $("#EMail").val();
        var userId = $("#UserId").val();
        var data = {
            EMail: email,
            FirstName: fname,
            LastName: lname,
            Id: userId
        };

        $.ajax({
            url: '/Account/UpdateUser',
            type: 'POST',
            data: data,
            dataType: 'json',
            success: OnUpdateUser
        });
    }

    function OnUpdateUser(result) {
        if (result.success) {
            alert("Update Success");
        }
        else {
            alert(result.errorMessage);
        }
    }

    function UpdatePassword() {
        var currentPassword = $("#CurrentPassword").val();
        var newPassword = $("#NewPassword").val();
        var confirmPassword = $("#ConfirmPassword").val();

        if (newPassword != confirmPassword) {
            alert("Please re-type your password correctly on the Confirm Password field");
        }
        else {
            var userId = $("#UserId").val();
            var data = {
                oldPassword: currentPassword,
                newPassword: newPassword,
                id: userId
            };

            $.ajax({
                url: '/Account/UpdatePassword',
                type: 'POST',
                data: data,
                dataType: 'json',
                success: OnUpdatePassword
            });
        }
    }

    function OnUpdatePassword(result) {
        if (result.success) {
            alert("Password Update Successful");
            $("#CurrentPassword").val("");
            $("#NewPassword").val("");
            $("#ConfirmPassword").val("");
        }
        else {
            alert(result.errorMessage);
        }
    }

    function GenerateLink() {
        var data = {
            url: $("#GeneratedReferralLink").val(),
            referralId: $("#ReferralId").text()
        };

        $.ajax({
            url: '/admin/GenerateLink',
            type: 'POST',
            data: data,
            dataType: 'json',
            success: OnGenerateLink
        });
    }

    function OnGenerateLink(result) {
        var link = $("#GeneratedLink");

        link.text(result.link);
    }

    var _instance = {
        updateUser: UpdateUser,
        updatePassword: UpdatePassword,
        generateLink: GenerateLink
    };

    return _instance;
})();